local mBindingsNLB = "|c00b3b3b3is no longer bound to|r"
local mBindingsINB = "|c00b3b3b3is now bound to|r"

if (GetLocale() == "zhCN") then
	mBindingsNLB = "|c00b3b3b3已解除绑定|r"
	mBindingsINB = "|c00b3b3b3已绑定到|r"
elseif (GetLocale() == "zhTW") then
	mBindingsNLB = "|c00b3b3b3已解除綁定|r"
	mBindingsINB = "|c00b3b3b3已綁定到|r"
end

mbindCSV = 1
mbindSV = {
	{
		["name"] = "default",
		["bindings"] = {},
	},
}
local id
local ids = 1
local mbindConfig = CreateFrame("Frame","mbindConfig", UIParent)
mbindConfig.name = "mBindings"
mbindConfig:SetWidth(350)
mbindConfig:SetHeight(400)
mbindConfig:Hide();
InterfaceOptions_AddCategory(mbindConfig)
local mbindbindslider = CreateFrame("Slider", "mbindbindslider", mbindConfig, "UIPanelScrollBarTemplate")
mbindbindslider:SetPoint("TOPRIGHT", -8, -40)
mbindbindslider:SetHeight(262)
mbindbindslider.last=1
local mbindbindContainer = CreateFrame("Frame", nil, mbindConfig)
mbindbindContainer:SetPoint("LEFT", 135, 0)
mbindbindContainer:SetHeight(300)
mbindbindContainer:SetPoint("TOPRIGHT", -5, -20)
mbindbindContainer:SetBackdrop({bgFile = nil, edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tile = true, tileSize = 32, edgeSize = 12, insets = { left = 11, right = 12, top = 12, bottom = 11 } } )
mbindConfig.listtext = {}
mbindConfig.nameheader = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontNormalLeft")
mbindConfig.nameheader:SetPoint("TOPLEFT", 140, -5)
mbindConfig.nameheader:SetText("command")
mbindConfig.bind1header = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontNormal")
mbindConfig.bind1header:SetPoint("TOPRIGHT", -100, -5)
mbindConfig.bind1header:SetText("key 2")
mbindConfig.bind1header:SetJustifyV("RIGHT")
mbindConfig.bind2header = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontNormal")
mbindConfig.bind2header:SetPoint("TOPRIGHT", -30, -5)
mbindConfig.bind2header:SetText("key 1")
mbindConfig.bind2header:SetJustifyV("RIGHT")
local mbindprofileslider = CreateFrame("Slider", "mbindprofileslider", mbindConfig, "UIPanelScrollBarTemplate")
mbindprofileslider:SetPoint("TOPLEFT", 116, -40)
mbindprofileslider:SetHeight(262)
mbindprofileslider.last=1
mbindprofileslider:SetScript("OnValueChanged", nil)
local mbindprofileContainer = CreateFrame("Frame", nil, mbindConfig)
mbindprofileContainer:SetPoint("TOPLEFT", 5, -20)
mbindprofileContainer:SetHeight(300)
mbindprofileContainer:SetPoint("RIGHT", mbindConfig, "LEFT", 135, 0)
mbindprofileContainer:SetBackdrop({bgFile = nil, edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tile = true, tileSize = 32, edgeSize = 12, insets = { left = 11, right = 12, top = 12, bottom = 11 } } )
mbindConfig.profileheader = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontNormalLeft")
mbindConfig.profileheader:SetPoint("TOPLEFT", 10, -5)
mbindConfig.profileheader:SetText("profile")
mbindConfig.listname = {}
mbindConfig.bind1 = {}
mbindConfig.bind2 = {}
mbindConfig.profile = {}
mbindConfig.bindClick = {}
mbindConfig.proClick = {}
mbindConfig.bindtexture = mbindConfig:CreateTexture(nil, "BACKGROUND")
mbindConfig.bindtexture:SetTexture("Interface\\Buttons\\UI-Listbox-Highlight2")
mbindConfig.bindtexture:SetVertexColor(1,1,1,0.3)
mbindConfig.bindtexture:SetBlendMode("ADD")
mbindConfig.protexture = mbindConfig:CreateTexture(nil, "BACKGROUND")
mbindConfig.protexture:SetTexture("Interface\\Buttons\\UI-Listbox-Highlight2")
mbindConfig.protexture:SetVertexColor(1,1,1,0.3)
mbindConfig.protexture:SetTexCoord(0.5,1.1,0,1)
mbindConfig.protexture:SetBlendMode("ADD")
mbindConfig.protexture.id = 1 --mbindCSV
mbindConfig.selAbility = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontNormalLargeLeft")
mbindConfig.selAbility:SetPoint("TOPLEFT", 140, -320)
mbindConfig.selBind1 = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontHighlightLeft")
mbindConfig.selBind1:SetPoint("TOPLEFT", 140, -340)
mbindConfig.selBind2 = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontHighlightRight")
mbindConfig.selBind2:SetPoint("TOPRIGHT", -10, -340)
for i=1, 15 do
	mbindConfig.listname[i] = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontHighlightLeft")
	mbindConfig.listname[i]:SetPoint("TOPLEFT", 140, -4-i*20)
	mbindConfig.bind2[i] = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontHighlightRight")
	mbindConfig.bind2[i]:SetPoint("TOPRIGHT", -100, -4-i*20)
	mbindConfig.bind2[i]:SetPoint("LEFT", mbindConfig.listname[i], "RIGHT", 0,0)
	mbindConfig.bind1[i] = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontHighlightRight")
	mbindConfig.bind1[i]:SetPoint("TOPRIGHT", -30, -4-i*20)
	mbindConfig.profile[i] = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontHighlightRight")
	mbindConfig.profile[i]:SetPoint("TOPLEFT", 10, -4-i*20)
	mbindConfig.bindClick[i] = CreateFrame("Frame", nil, mbindConfig)
	mbindConfig.bindClick[i]:SetPoint("TOPLEFT", 138, -i*20)
	mbindConfig.bindClick[i]:SetPoint("RIGHT", -8, 0)
	mbindConfig.bindClick[i]:SetHeight(20)
	mbindConfig.bindClick[i].id = i
	mbindConfig.bindClick[i]:SetScript("OnMouseUp", function(self)
		mbindConfig.bindtexture:SetAllPoints(self)
		mbindConfig.bindtexture:Show()
		mbindConfig.bindtexture.id = self.id
		id = self.id+mbindbindslider:GetValue()-1
		mbindConfig.selAbility:SetText(mbindSV[ids].bindings[id].name)
		mbindConfig.selBind1:SetText(mbindSV[ids].bindings[id].bind1)
		mbindConfig.selBind2:SetText(mbindSV[ids].bindings[id].bind2)
	end)
	mbindConfig.bindClick[i]:SetScript("OnMouseWheel", function(self, d)
		if mbindbindslider:GetValue()-d>#mbindSV[ids].bindings or mbindbindslider:GetValue()-d<1 then return end
		mbindbindslider:SetValue(mbindbindslider:GetValue()-d)
	end)
	mbindConfig.bindClick[i]:EnableMouseWheel(true)
	mbindConfig.proClick[i] = CreateFrame("Frame", nil, mbindConfig)
	mbindConfig.proClick[i]:SetPoint("TOPLEFT", 7, -i*20)
	mbindConfig.proClick[i]:SetPoint("RIGHT", mbindConfig, "TOPLEFT", 135, 0)
	mbindConfig.proClick[i]:SetHeight(20)
	mbindConfig.proClick[i].id = i
	mbindConfig.proClick[i]:SetScript("OnMouseUp", function(self)
		mbindConfig.bindtexture:Hide()
		mbindConfig.bindtexture.id = 1
		mbindConfig.protexture:SetAllPoints(self)
		mbindConfig.protexture:Show()
		mbindConfig.protexture.id = self.id
		ids = self.id+mbindbindslider:GetValue()-1
		updateBindProfile()
	end)
	mbindConfig.proClick[i]:SetScript("OnMouseWheel", function(self, d)
		if mbindprofileslider:GetValue()-d>#mbindSV or mbindprofileslider:GetValue()-d<1 then return end
		mbindprofileslider:SetValue(mbindprofileslider:GetValue()-d)
	end)
	mbindConfig.proClick[i]:EnableMouseWheel(true)
	--[[mbindConfig.texture[i] = mbindConfig:CreateTexture(nil, "BACKGROUND")
	mbindConfig.texture[i]:SetWidth(200)
	mbindConfig.texture[i]:SetHeight(10)
	mbindConfig.texture[i]:SetPoint("TOPLEFT", 0, -((i-1)*20))
	mbindConfig.texture[i]:SetTexture(1, 1, 1, 0.2)]]
end

function updateBindProfile()
	local offset = mbindprofileslider:GetValue()-1
	for i=1, 15 do
		if mbindSV[i+offset] then
			if i+offset == mbindCSV then
				mbindConfig.profile[i]:SetText(">"..mbindSV[i+offset].name)
			else
				mbindConfig.profile[i]:SetText(mbindSV[i+offset].name)
			end
			mbindConfig.proClick[i]:EnableMouse(true)
		else
			mbindConfig.profile[i]:SetText(nil)
			mbindConfig.proClick[i]:EnableMouse(false)
		end
	end
	mbindprofileslider:SetMinMaxValues(1, #mbindSV>15 and #mbindSV-14 or 1)
	if mbindprofileslider:GetValue() > #mbindSV then mbindprofileslider:SetValue(#mbindSV) end
	offset = mbindbindslider:GetValue()-1
	for i=1, 15 do
		if mbindSV[ids].bindings[i+offset] then
			mbindConfig.listname[i]:SetText(mbindSV[ids].bindings[i+offset].name)
			mbindConfig.bind1[i]:SetPoint("LEFT", mbindConfig.listname[i], "RIGHT", 0,0)
			if mbindSV[ids].bindings[i+offset].bind2 then
				if not mbindSV[ids].bindings[i+offset].bind1 then
					mbindSV[ids].bindings[i+offset].bind1 = mbindSV[ids].bindings[i+offset].bind2
					mbindSV[ids].bindings[i+offset].bind2 = nil
				else
					mbindConfig.bind2[i]:SetText(mbindSV[ids].bindings[i+offset].bind2)
					mbindConfig.bind1[i]:SetPoint("LEFT", mbindConfig.bind2[i], "RIGHT", 0,0)
				end
			else
				mbindConfig.bind2[i]:SetText(mbindSV[ids].bindings[i+offset].bind2)
			end
			mbindConfig.bind1[i]:SetText(mbindSV[ids].bindings[i+offset].bind1)
			mbindConfig.bindClick[i]:EnableMouse(true)
		else
			mbindConfig.listname[i]:SetText(nil)
			mbindConfig.bind1[i]:SetText(nil)
			mbindConfig.bind2[i]:SetText(nil)
			mbindConfig.bindClick[i]:EnableMouse(false)
		end
	end
	mbindbindslider:SetMinMaxValues(1, #mbindSV[ids].bindings>15 and #mbindSV[ids].bindings-14 or 1)
	if mbindbindslider:GetValue() > #mbindSV[ids].bindings then mbindbindslider:SetValue(#mbindSV[ids].bindings) end
end


mbindConfig.addBut = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.addBut:SetText("add")
mbindConfig.addBut:SetPoint("BOTTOMRIGHT", -5, 5)
mbindConfig.addBut:SetWidth(40)
mbindConfig.addBut:SetScript("OnMouseUp", function()
	if mbindConfig.bndBut:GetText() == "key" or mbindConfig.cmdBox:GetText() == "command" or mbindConfig.cmdBox:GetText() == "" then return end
	local exist
	for i=1, #mbindSV[ids].bindings do
		if mbindSV[ids].bindings[i].name == mbindConfig.cmdBox:GetText() then
			mbindSV[ids].bindings[i].bind2 = mbindConfig.bndBut:GetText()
			exist = 1
			break
		end
	end
	if not exist then
		table.insert(mbindSV[ids].bindings,{name = mbindConfig.cmdBox:GetText(), bind1 = mbindConfig.bndBut:GetText()})
	end
	if id then
		mbindConfig.selAbility:SetText(mbindSV[ids].bindings[id].name)
		mbindConfig.selBind1:SetText(mbindSV[ids].bindings[id].bind1)
		mbindConfig.selBind2:SetText(mbindSV[ids].bindings[id].bind2)
	end
	mbindConfig.bndBut:SetText("key")
	updateBindProfile()
end)

mbindConfig.bndBut = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.bndBut:SetText("key")
mbindConfig.bndBut:SetPoint("RIGHT", mbindConfig.addBut, "LEFT", 0, 0)
mbindConfig.bndBut:SetWidth(60)
mbindConfig.bndBut:SetScript("OnEnter", function()
	GameTooltip:SetOwner(InterfaceOptionsFrameCancel, "ANCHOR_BOTTOMLEFT", 0, 30)
	GameTooltip:SetText("This button is used to create a key combination to be used with the\n command in the editbox to the left. Click once then press a key.")
	GameTooltip:Show()
end)
mbindConfig.bndBut:SetScript("OnMouseUp", function(self, button)
	mbindConfig.cmdBox:ClearFocus()
	if self:IsKeyboardEnabled() then
		self:EnableKeyboard(false)
		self:UnlockHighlight()
		if ( button == "MiddleButton" ) then
			button = "BUTTON3";
		elseif ( button == "Button4" ) then
			button = "BUTTON4"
		elseif ( button == "Button5" ) then
			button = "BUTTON5"
		else
			return;
		end

		if ( IsShiftKeyDown() ) then
			button = "SHIFT-"..button;
		end
		if ( IsControlKeyDown() ) then
			button = "CTRL-"..button;
		end
		if ( IsAltKeyDown() ) then
			button = "ALT-"..button;
		end
		self:SetText(button)
	else
		self:EnableKeyboard(true)
		self:LockHighlight()
	end
end)
mbindConfig.bndBut:SetScript("OnKeyDown", function(self, keyOrButton)
	if ( GetBindingByKey(keyOrButton) == "SCREENSHOT" ) then
		RunBinding("SCREENSHOT");
		return;
	end

	if ( keyOrButton == "LeftButton" ) then
		keyOrButton = "BUTTON1";
	elseif ( keyOrButton == "RightButton" ) then
		keyOrButton = "BUTTON2";
	elseif ( keyOrButton == "MiddleButton" ) then
		keyOrButton = "BUTTON3";
	elseif ( keyOrButton == "Button4" ) then
		keyOrButton = "BUTTON4"
	elseif ( keyOrButton == "Button5" ) then
		keyOrButton = "BUTTON5"
	end

	local keyPressed = keyOrButton;

	if ( keyOrButton ) then
		if ( keyOrButton == "BUTTON1" or keyOrButton == "BUTTON2" ) then
			return;
		end
		keyPressed = keyOrButton;
	else
		keyPressed = keyOrButton;
	end

	if ( keyPressed == "UNKNOWN" ) then
		return;
	end

	if ( keyPressed == "LSHIFT" or
	     keyPressed == "RSHIFT" or
	     keyPressed == "LCTRL" or
	     keyPressed == "RCTRL" or
	     keyPressed == "LALT" or
	     keyPressed == "RALT" ) then
		return;
	end

	if ( IsShiftKeyDown() ) then
		keyPressed = "SHIFT-"..keyPressed;
	end
	if ( IsControlKeyDown() ) then
		keyPressed = "CTRL-"..keyPressed;
	end
	if ( IsAltKeyDown() ) then
		keyPressed = "ALT-"..keyPressed;
	end
	self:SetText(keyPressed)
	self:EnableKeyboard(false)
	self:UnlockHighlight()
end)
mbindConfig.bndBut:SetScript("OnLeave", function()
	GameTooltip:Hide()
end)

mbindConfig.cmdBox = CreateFrame("EditBox", "mbcmdBox", mbindConfig, "InputBoxTemplate")
mbindConfig.cmdBox:SetWidth(150)
mbindConfig.cmdBox:SetHeight(16)
mbindConfig.cmdBox:SetText("command")
mbindConfig.cmdBox:SetPoint("BOTTOMLEFT", 140, 7)
mbindConfig.cmdBox:SetPoint("RIGHT", mbindConfig.bndBut, "LEFT", 0, 0)
mbindConfig.cmdBox:SetAutoFocus(false)
mbindConfig.cmdBox:SetScript("OnEnterPressed", function(self)
	self:ClearFocus()
end)

mbindConfig.rmvBnd = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.rmvBnd:SetText("binding")
mbindConfig.rmvBnd:SetPoint("BOTTOMLEFT", 135, 40)
mbindConfig.rmvBnd:SetWidth(100)
mbindConfig.rmvBnd:SetScript("OnEnter", function()
	GameTooltip:SetOwner(InterfaceOptionsFrameCancel, "ANCHOR_BOTTOMLEFT", 0, 30)
	GameTooltip:SetText("Click to remove the selected binding from the selected profile.")
	GameTooltip:Show()
end)
mbindConfig.rmvBnd:SetScript("OnMouseUp", function()
	table.remove(mbindSV[ids].bindings, mbindConfig.bindtexture.id+mbindbindslider:GetValue()-1)
	print(ids)
	print(mbindConfig.bindtexture.id+mbindbindslider:GetValue()-1)
	mbindConfig.bindtexture:Hide()
	mbindConfig.selAbility:SetText(nil)
	mbindConfig.selBind1:SetText(nil)
	mbindConfig.selBind2:SetText(nil)
	updateBindProfile()
end)
mbindConfig.rmvBnd:SetScript("OnLeave", function()
	GameTooltip:Hide()
end)

mbindConfig.rmvkey1 = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.rmvkey1:SetText("key 1")
mbindConfig.rmvkey1:SetPoint("LEFT", mbindConfig.rmvBnd, "RIGHT", 7, 0)
mbindConfig.rmvkey1:SetWidth(80)
mbindConfig.rmvkey1:SetScript("OnEnter", function()
	GameTooltip:SetOwner(InterfaceOptionsFrameCancel, "ANCHOR_BOTTOMLEFT", 0, 30)
	GameTooltip:SetText("Click to remove the first key of the selected binding from the selected profile.")
	GameTooltip:Show()
end)
mbindConfig.rmvkey1:SetScript("OnMouseUp", function()
	local a, b = ids, mbindConfig.bindtexture.id+mbindbindslider:GetValue()-1
	if mbindSV[a].bindings[b].bind2 then
		mbindSV[a].bindings[b].bind1 = mbindSV[a][b].bind2
		mbindSV[a].bindings[b].bind2 = nil
		mbindConfig.selBind2:SetText(nil)
	else
		table.remove(mbindSV[a].bindings, b)
		mbindConfig.selAbility:SetText(nil)
		mbindConfig.selBind1:SetText(nil)
		mbindConfig.selBind2:SetText(nil)
	end
	updateBindProfile()
end)
mbindConfig.rmvkey1:SetScript("OnLeave", function()
	GameTooltip:Hide()
end)

mbindConfig.rmvkey2 = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.rmvkey2:SetText("key 2")
mbindConfig.rmvkey2:SetPoint("LEFT", mbindConfig.rmvkey1, "RIGHT", 7, 0)
mbindConfig.rmvkey2:SetWidth(80)
mbindConfig.rmvkey2:SetScript("OnEnter", function()
	GameTooltip:SetOwner(InterfaceOptionsFrameCancel, "ANCHOR_BOTTOMLEFT", 0, 30)
	GameTooltip:SetText("Click to remove the second key of the selected binding from the selected profile.")
	GameTooltip:Show()
end)
mbindConfig.rmvkey2:SetScript("OnMouseUp", function()
	mbindSV[ids].bindings[mbindConfig.bindtexture.id+mbindbindslider:GetValue()-1].bind2 = nil
	updateBindProfile()
	mbindConfig.selBind2:SetText(nil)
end)
mbindConfig.rmvkey2:SetScript("OnLeave", function()
	GameTooltip:Hide()
end)

mbindConfig.lodPro = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.lodPro:SetText("load profile")
mbindConfig.lodPro:SetPoint("BOTTOMLEFT", 3, 88)
mbindConfig.lodPro:SetWidth(130)
mbindConfig.lodPro:SetScript("OnMouseUp", function()
	mbindCSV = ids+mbindprofileslider:GetValue()-1
	--print(#(mbindSV[mbindCSV].bindings))
	for i=1, #(mbindSV[mbindCSV].bindings) do
		--print(mbindSV[mbindCSV].bindings[i].name)
		SetBinding(mbindSV[mbindCSV].bindings[i].bind1, mbindSV[mbindCSV].bindings[i].name)
		if mbindSV[mbindCSV].bindings[i].bind2 then
			SetBinding(mbindSV[mbindCSV].bindings[i].bind2, mbindSV[mbindCSV].bindings[i].name)
		end
	end
	updateBindProfile()
end)

mbindConfig.boxPro = CreateFrame("EditBox", "mbBoxPro", mbindConfig, "InputBoxTemplate")
mbindConfig.boxPro:SetWidth(120)
mbindConfig.boxPro:SetHeight(16)
mbindConfig.boxPro:SetText("profile")
mbindConfig.boxPro:SetPoint("TOP", mbindConfig.lodPro, "BOTTOM", 3, -3)
mbindConfig.boxPro:SetAutoFocus(false)
mbindConfig.boxPro:SetScript("OnEnterPressed", function(self)
	self:ClearFocus()
end)


mbindConfig.newPro = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.newPro:SetText("new profile")
mbindConfig.newPro:SetPoint("TOP", mbindConfig.boxPro, "BOTTOM", -3, -3)
mbindConfig.newPro:SetWidth(130)
mbindConfig.newPro:SetScript("OnMouseUp", function()
	table.insert(mbindSV, {name = mbindConfig.boxPro:GetText(), bindings = {}})
	updateBindProfile()
end)

mbindConfig.cpyPro = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.cpyPro:SetText("copy profile")
mbindConfig.cpyPro:SetPoint("TOP", mbindConfig.newPro, "BOTTOM", 0, 0)
mbindConfig.cpyPro:SetWidth(130)
mbindConfig.cpyPro:SetScript("OnMouseUp", function()
	table.insert(mbindSV, {name = mbindConfig.boxPro:GetText(), bindings = mbindSV[ids+mbindprofileslider:GetValue()-1].bindings})
	updateBindProfile()
end)

mbindConfig.delPro = CreateFrame("Button", nil, mbindConfig, "OptionsButtonTemplate")
mbindConfig.delPro:SetText("delete profile")
mbindConfig.delPro:SetPoint("TOP", mbindConfig.cpyPro, "BOTTOM", 0, 0)
mbindConfig.delPro:SetWidth(130)
mbindConfig.delPro:SetScript("OnMouseUp", function()
	table.remove(mbindSV, ids+mbindprofileslider:GetValue()-1)
	ids=1
	mbindConfig.protexture:SetAllPoints(mbindConfig.proClick[1])
	mbindConfig.protexture.id = 1
	updateBindProfile()
end)

mbindConfig.bndText = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontNormalLeft")
mbindConfig.bndText:SetPoint("BOTTOMLEFT", 140, 27)
mbindConfig.bndText:SetText("add")
mbindConfig.bndText:SetJustifyH("RIGHT")

mbindConfig.rmvText = mbindConfig:CreateFontString(nil, "OVERLAY", "GameFontNormalLeft")
mbindConfig.rmvText:SetPoint("BOTTOMLEFT", 140, 62)
mbindConfig.rmvText:SetText("remove")
mbindConfig.rmvText:SetJustifyH("RIGHT")

mbindConfig:SetScript("OnEvent", function()
updateBindProfile()
mbindbindslider:SetMinMaxValues(1, 100)
mbindbindslider:SetScript("OnValueChanged", function(self)
	if mbindConfig.bindtexture.id then
		mbindConfig.bindtexture.id = mbindConfig.bindtexture.id-(self:GetValue()-self.last)
		if mbindConfig.bindtexture.id>15 or mbindConfig.bindtexture.id<1 then
			mbindConfig.bindtexture:Hide()
		else
			mbindConfig.bindtexture:Show()
		end
		mbindConfig.bindtexture:SetAllPoints(mbindConfig.bindClick[mbindConfig.bindtexture.id])
	end
	self.last=self:GetValue()
	updateBindProfile()
end)
mbindbindslider:SetValue(1)
mbindbindslider:SetValueStep(1)
mbindprofileslider:SetMinMaxValues(1, 2)
mbindprofileslider:SetValue(1)
mbindprofileslider:SetValueStep(1)
mbindprofileslider:SetScript("OnValueChanged", function(self)
	if mbindConfig.protexture.id then
		mbindConfig.protexture.id = mbindConfig.protexture.id-(self:GetValue()-self.last)
		if mbindConfig.protexture.id>15 or mbindConfig.protexture.id<1 then
			mbindConfig.protexture:Hide()
		else
			mbindConfig.protexture:Show()
		end
		mbindConfig.protexture:SetAllPoints(mbindConfig.proClick[mbindConfig.protexture.id])
	end
	self.last=self:GetValue()
	updateBindProfile()
end)
ids = mbindCSV
if ids<15 then
	mbindConfig.protexture:SetAllPoints(mbindConfig.proClick[ids])
	mbindConfig.protexture:Show()
	mbindConfig.protexture.id = mbindConfig.proClick[ids].id
end
updateBindProfile()
end)
mbindConfig:RegisterEvent("VARIABLES_LOADED")



local updateMacroBind
local bindBut = CreateFrame("CheckButton", "bindBut", UIParent, "UICheckButtonTemplate");
local keyFrame = CreateFrame("Button", nil, bindBut);
 keyFrame:EnableKeyboard();
 keyFrame:Hide();
 bindButText:SetText("enable bindings");
  if (GetLocale()=="zhCN") then  bindButText:SetText("快速绑定按键"); elseif (GetLocale() == "zhTW") then  bindButText:SetText("快速綁定按鍵");end
 --bindBut:SetPoint("TOPLEFT", 74, -38);
 --ShowAllSpellRanksCheckBox:ClearAllPoints();
 --ShowAllSpellRanksCheckBox:SetPoint("LEFT", bindButText, "RIGHT", 10, 0);
 bindBut:SetWidth(26);
 bindBut:SetHeight(26);
 bindBut:SetFrameStrata("HIGH")
 bindBut:SetScript("OnMouseUp", function()
	if bindBut:GetChecked() then
		keyFrame:Hide();
	else
		keyFrame:Show();
	end
 end);
 bindBut:SetScript("OnEnter", function(self)
 GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
 GameTooltip:SetText("Click to start/stop binding abilities.\nMouseover an ability and press a button to bind them together.\nPress esc to clear all bindings for an ability.");
 if (GetLocale() == "zhCN") then	 GameTooltip:SetText("点击开始/停止绑定设置。\n鼠标移动到技能上按下按键设置绑定。\n按Esc键清除按键绑定。");
elseif (GetLocale() == "zhTW") then  GameTooltip:SetText("點擊開始/停止綁定設置。\n滑鼠移動到技能上按下按鍵設置綁定。\n按Esc鍵清除按鍵綁定。");end
GameTooltip:Show();
 end);
 bindBut:SetScript("OnLeave", function(self)
 GameTooltip:Hide();
 end);
 SpellBookFrame:HookScript("OnShow", function(self)
	bindBut:Show()
	bindBut:ClearAllPoints()
	bindBut:SetPoint("TOPLEFT", self, 200, -38)

 end)

 SpellBookFrame:HookScript("OnHide", function(self)
	if MacroFrame and MacroFrame:IsShown() then
		bindBut:ClearAllPoints()
		bindBut:SetPoint("LEFT", MacroFrame, 200, -38)
	else
		bindBut:Hide()
	end
 end)


local macro = CreateFrame("Frame")
macro:SetScript("OnEvent", function()
	if arg1 ~= "Blizzard_MacroUI" then return end
	local selMacroText = MacroFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightLeft")
	selMacroText:SetPoint("TOPLEFT", MacroFrameSelectedMacroBackground, 200, 0)
	selMacroText:SetWidth(120)
	selMacroText:SetHeight(30)
	--selMacroText:SetJustifyV("BOTTOM")
	updateMacroBind = function()
		local bind1, bind2 = GetBindingKey("MACRO "..(GetMacroInfo(MacroFrame.selectedMacro)))
		selMacroText:SetText(bind2 and bind1..", "..bind2 or bind1)
		local frame
		for i=1, 36 do
			frame = _G["MacroButton"..i]
			bind1 = (GetMacroInfo(MacroFrame.macroBase + frame:GetID())) and GetBindingKey("MACRO "..(GetMacroInfo(MacroFrame.macroBase + frame:GetID()))) or nil
			frame.bindText:SetText(bind1 and bind1:gsub("SHIFT", "S"):gsub("CTRL", "C"):gsub("ALT", "A"):gsub("BUTTON", "B"):sub(1, 8) or nil)
		end
	end
	hooksecurefunc("MacroFrame_Update", updateMacroBind)
	MacroFrame:HookScript("OnShow", function(self)
		bindBut:ClearAllPoints()
		bindBut:SetPoint("LEFT", self, 200, -38)
		bindBut:Show()
	end)
	MacroFrame:HookScript("OnHide", function(self)
		if SpellBookFrame:IsShown() then
			bindBut:ClearAllPoints()
			bindBut:SetPoint("TOPLEFT", SpellBookFrame, 200, -38)
		else
			bindBut:Hide()
		end
	end)
	local function setupMacroButton(frame)
	frame.bindText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightRight")
	frame.bindText:SetPoint("TOPRIGHT", -3, -3)
	frame.mBind = "macro"
	frame:SetScript("OnMouseDown", function(self,button)
	if not keyFrame:IsShown() then return end
	if ( button == "MiddleButton" ) then
		button = "BUTTON3";
	elseif ( button == "Button4" ) then
		button = "BUTTON4"
	elseif ( button == "Button5" ) then
		button = "BUTTON5"
	else
		return;
	end

	if ( IsShiftKeyDown() ) then
		button = "SHIFT-"..button;
	end
	if ( IsControlKeyDown() ) then
		button = "CTRL-"..button;
	end
	if ( IsAltKeyDown() ) then
		button = "ALT-"..button;
	end
	local macro = GetMacroInfo(MacroFrame.macroBase + frame:GetID())
	if ( keyOrButton == "ESCAPE" ) then
		local key1, key2 = GetBindingKey("MACRO "..macro);
		if key1 then
			SetBinding(key1);
			DEFAULT_CHAT_FRAME:AddMessage(key1.." "..mBindingsNLB.." MACRO "..macro);
		end
		if key2 then
			SetBinding(key2);
			DEFAULT_CHAT_FRAME:AddMessage(key2.." "..mBindingsNLB.." MACRO "..macro);
		end
	else
		local oldAction = GetBindingAction(button);
		if oldAction ~= "" then
			DEFAULT_CHAT_FRAME:AddMessage(button.." "..mBindingsNLB.." "..oldAction);
		end
		SetBindingMacro(button, macro);
		DEFAULT_CHAT_FRAME:AddMessage(button.." "..mBindingsINB.." MACRO "..macro);
	end
	updateMacroBind()
	SaveBindings(2);
	end)
end
	for i=1, 36 do
		setupMacroButton(_G["MacroButton"..i])
	end
end)
macro:RegisterEvent("ADDON_LOADED")


local function bindFunc(self, keyOrButton)
	local mouseFocus = GetMouseFocus();
	if not mouseFocus.mBind then return end

	if ( GetBindingByKey(keyOrButton) == "SCREENSHOT" ) then
		RunBinding("SCREENSHOT");
		return;
	end

	-- Convert the mouse button names
	if ( keyOrButton == "LeftButton" ) then
		keyOrButton = "BUTTON1";
	elseif ( keyOrButton == "RightButton" ) then
		keyOrButton = "BUTTON2";
	elseif ( keyOrButton == "MiddleButton" ) then
		keyOrButton = "BUTTON3";
	elseif ( keyOrButton == "Button4" ) then
		keyOrButton = "BUTTON4"
	elseif ( keyOrButton == "Button5" ) then
		keyOrButton = "BUTTON5"
	end

	local keyPressed = keyOrButton;

	if ( keyOrButton ) then
		if ( keyOrButton == "BUTTON1" or keyOrButton == "BUTTON2" ) then
			return;
		end
		keyPressed = keyOrButton;
	else
		keyPressed = keyOrButton;
	end

	if ( keyPressed == "UNKNOWN" ) then
		return;
	end

	if ( keyPressed == "LSHIFT" or
	     keyPressed == "RSHIFT" or
	     keyPressed == "LCTRL" or
	     keyPressed == "RCTRL" or
	     keyPressed == "LALT" or
	     keyPressed == "RALT" ) then
		return;
	end

	if ( IsShiftKeyDown() ) then
		keyPressed = "SHIFT-"..keyPressed;
	end
	if ( IsControlKeyDown() ) then
		keyPressed = "CTRL-"..keyPressed;
	end
	if ( IsAltKeyDown() ) then
		keyPressed = "ALT-"..keyPressed;
	end

	if mouseFocus.mBind == "spell" then
		local spellName = GetSpellName(SpellBook_GetSpellID(mouseFocus:GetID()), SpellBookFrame.bookType);
		if spellName then
			if ( keyOrButton == "ESCAPE" ) then
				local key1, key2 = GetBindingKey("SPELL "..spellName);
				if key1 then
					SetBinding(key1);
					DEFAULT_CHAT_FRAME:AddMessage(key1.." "..mBindingsNLB.." "..spellName);
				end
				if key2 then
					SetBinding(key2);
					DEFAULT_CHAT_FRAME:AddMessage(key2.." "..mBindingsNLB.." "..spellName);
				end
				for i=1, #(mbindSV[mbindCSV].bindings) do
					if mbindSV[mbindCSV].bindings[i].name == ("SPELL "..spellName) then
						table.remove(mbindSV[mbindCSV].bindings, i)
						updateBindProfile()
						break
					end
				end
			else
				local oldAction = GetBindingAction(keyPressed);
				if oldAction ~= "" then
					DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." "..mBindingsNLB.." "..oldAction);
				end
				for i=1, #(mbindSV[mbindCSV].bindings) do
					if mbindSV[mbindCSV].bindings[i].bind1 == keyPressed then
						if mbindSV[mbindCSV].bindings[i].bind2 then
							mbindSV[mbindCSV].bindings[i].bind1 = mbindSV[mbindCSV].bindings[i].bind2
							mbindSV[mbindCSV].bindings[i].bind2 = nil
						else
							table.remove(mbindSV[mbindCSV].bindings, i)
							updateBindProfile()
							break
						end
					elseif mbindSV[mbindCSV].bindings[i].bind2 == keyPressed then
						mbindSV[mbindCSV].bindings[i].bind2 = nil
					end
				end
				local saywut
				for i=1, #(mbindSV[mbindCSV].bindings) do
					if mbindSV[mbindCSV].bindings[i].name == ("SPELL "..spellName) then
						mbindSV[mbindCSV].bindings[i].bind2 = keyPressed
						saywut = 1
					end
				end
				if not saywut then
					table.insert(mbindSV[mbindCSV].bindings,{name = "SPELL "..spellName, bind1 = keyPressed})
				end
				updateBindProfile()
				SetBindingSpell(keyPressed, spellName);
				DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." "..mBindingsINB.." "..spellName);
			end
			SpellButton_OnEnter(mouseFocus);
			SaveBindings(2);
		end
	elseif mouseFocus.mBind == "macro" then
		local macro = GetMacroInfo(MacroFrame.macroBase + mouseFocus:GetID())
		if ( keyOrButton == "ESCAPE" ) then
			local key1, key2 = GetBindingKey("MACRO "..macro);
			if key1 then
				SetBinding(key1);
				DEFAULT_CHAT_FRAME:AddMessage(key1.." "..mBindingsNLB.." MACRO "..macro);
			end
			if key2 then
				SetBinding(key2);
				DEFAULT_CHAT_FRAME:AddMessage(key2.." "..mBindingsNLB.." MACRO "..macro);
			end
			for i=1, #(mbindSV[mbindCSV].bindings) do
				if mbindSV[mbindCSV].bindings[i].name == ("MACRO "..macro) then
					table.remove(mbindSV[mbindCSV].bindings, i)
					updateBindProfile()
					break
				end
			end
		else
			local oldAction = GetBindingAction(keyPressed);
			if oldAction ~= "" then
				DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." "..mBindingsNLB.." "..oldAction);
			end
			SetBindingMacro(keyPressed, macro);
			DEFAULT_CHAT_FRAME:AddMessage(keyPressed.." "..mBindingsINB.." MACRO "..macro);
			for i=1, #(mbindSV[mbindCSV].bindings) do
				if mbindSV[mbindCSV].bindings[i].bind1 == keyPressed then
					if mbindSV[mbindCSV].bindings[i].bind2 then
						mbindSV[mbindCSV].bindings[i].bind1 = mbindSV[mbindCSV].bindings[i].bind2
						mbindSV[mbindCSV].bindings[i].bind2 = nil
					else
						table.remove(mbindSV[mbindCSV].bindings, i)
						updateBindProfile()
						break
					end
				elseif mbindSV[mbindCSV].bindings[i].bind2 == keyPressed then
					mbindSV[mbindCSV].bindings[i].bind2 = nil
				end
			end
			local saywut
			for i=1, #(mbindSV[mbindCSV].bindings) do
				if mbindSV[mbindCSV].bindings[i].name == ("MACRO "..macro) then
					mbindSV[mbindCSV].bindings[i].bind2 = keyPressed
					saywut = 1
				end
			end
			if not saywut then
				table.insert(mbindSV[mbindCSV].bindings,{name = "MACRO "..macro, bind1 = keyPressed})
			end
			updateBindProfile()
		end
		updateMacroBind()
		SaveBindings(2);
	end
end
keyFrame:SetScript("OnKeyDown", bindFunc)

local function setupSpellButton(frame)
	frame.mBind = "spell"
	frame:SetScript("OnMouseDown", function(self,button)
	if keyFrame:IsShown() then
	if ( button == "MiddleButton" ) then
		button = "BUTTON3";
	elseif ( button == "Button4" ) then
		button = "BUTTON4"
	elseif ( button == "Button5" ) then
		button = "BUTTON5"
	else
		return;
	end

	if ( IsShiftKeyDown() ) then
		button = "SHIFT-"..button;
	end
	if ( IsControlKeyDown() ) then
		button = "CTRL-"..button;
	end
	if ( IsAltKeyDown() ) then
		button = "ALT-"..button;
	end
	local spellName = GetSpellName(SpellBook_GetSpellID(self:GetID()), SpellBookFrame.bookType);
	if spellName then
		if ( keyOrButton == "ESCAPE" ) then
			local key1, key2 = GetBindingKey("SPELL "..spellName);
			if key1 then
				SetBinding(key1);
				DEFAULT_CHAT_FRAME:AddMessage(key1.." "..mBindingsNLB.." "..spellName);
			end
			if key2 then
				SetBinding(key2);
				DEFAULT_CHAT_FRAME:AddMessage(key2.." "..mBindingsNLB.." "..spellName);
			end
		else
			local oldAction = GetBindingAction(button);
			if oldAction ~= "" then
				DEFAULT_CHAT_FRAME:AddMessage(button.." "..mBindingsNLB.." "..oldAction);
			end
			SetBindingSpell(button, spellName);
			DEFAULT_CHAT_FRAME:AddMessage(button.." "..mBindingsINB.." "..spellName);
			local saywut
			for i=1, #(mbindSV[mbindCSV].bindings) do
				if mbindSV[mbindCSV].bindings[i].name == ("SPELL "..spellName) then
					mbindSV[mbindCSV].bindings[i].bind2 = button
					saywut = 1
				end
			end
			if not saywut then
				table.insert(mbindSV[mbindCSV].bindings,{name = "SPELL "..spellName, bind1 = button})
			end
			updateBindProfile()
		end
	SpellButton_OnEnter(self);
	SaveBindings(2);
	end
	end
end)
end



 for i=1, 12 do
	setupSpellButton(_G["SpellButton"..i])
 end

 local function hookTooltip(self)
	local focus = GetMouseFocus()
	if not focus.mBind then return end
	local id = SpellBook_GetSpellID(focus:GetID())
	if not GetSpellName(id, SpellBookFrame.bookType) then return end
	local bind1, bind2 = GetBindingKey("SPELL "..GetSpellName(id, SpellBookFrame.bookType))
	if bind1 then
		if bind2 then
		GameTooltip:AddLine(bind1..", "..bind2, 1, 1, 1)
		else
		GameTooltip:AddLine(bind1, 1, 1, 1)
		end
		GameTooltip:Show()
	end
end
GameTooltip:HookScript("OnTooltipSetSpell", hookTooltip)